package de.jenseins.utils.propertytranslator.gui;

import java.awt.Color;
import java.awt.Component;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import com.google.api.translate.Language;

import de.jenseins.utils.propertytranslator.gui.listener.ClickListener;
import de.jenseins.utils.propertytranslator.gui.listener.ExitApplicationListener;
import de.jenseins.utils.propertytranslator.gui.listener.OpenPropertyFileListener;
import de.jenseins.utils.propertytranslator.gui.listener.StartTranslationListener;
import de.jenseins.utils.propertytranslator.service.LanguageManager;

/**
 * 
 * 
 * @author Jens Greive
 * @since 02.12.2009
 * 
 */
public class MainFrame extends JFrame implements IHaveProgressBar {

	private static final long serialVersionUID = -6433319956852359842L;

	private final int width = 400;

	private final int height = 377;

	private JTextField selectedFileField = new JTextField();

	private JScrollPane sourceLangSrollPane;

	private JScrollPane targetLangSrollPane;

	private JList sourceLanguageList;

	private JList targetLanguageList;

	private JButton translateButton;

	private JButton openButton;

	private OpenPropertyFileListener openPropertyFileLister;
	private ClickListener clickListener;

	private JProgressBar pbar;

	private JLabel statusLabel;

	private static final String[] languages = LanguageManager.getLanguages();

	public MainFrame() {
		super("Property file translator");
		openPropertyFileLister = new OpenPropertyFileListener(selectedFileField, this);
		clickListener = new ClickListener(this);
		init();
		setVisible(true);
	}

	private void init() {
		setSize(width, height);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setLayout(null);
		setResizable(false);
		setJMenuBar(getMenu());
		add(getSelectLanguagesPanel());
		add(getOpenFilePanel());
		add(getButtonPanel());
		add(getProgressPanel());
	}

	/**
	 * @return
	 */
	private Component getProgressPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(0, 288, width, 44);

		statusLabel = new JLabel();
		statusLabel.setBounds(10, 0, width, 22);

		pbar = new JProgressBar(0, 100);
		pbar.setStringPainted(true);
		pbar.setValue(0);
		pbar.setBounds(0, 22, width, 22);
		panel.add(statusLabel);
		panel.add(pbar);
		return panel;
	}

	private JPanel getButtonPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(0, 253, width, 30);
		translateButton = new JButton("Translate");
		translateButton.setBounds(150, 5, 100, 22);
		translateButton.setEnabled(false);
		translateButton.addActionListener(new StartTranslationListener(this));
		panel.add(translateButton);
		return panel;
	}

	private JPanel getSelectLanguagesPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(0, 0, width, 200);
		JLabel label = new JLabel("Source language:");
		label.setBounds(10, 20, 150, 22);
		panel.add(label);
		label = new JLabel("Target languages:");
		label.setBounds(210, 20, 150, 22);
		panel.add(label);
		panel.setBorder(null);
		initSourceLanguageList();
		initTargetLanguageList();
		panel.add(sourceLangSrollPane);
		panel.add(targetLangSrollPane);
		return panel;
	}

	private JPanel getOpenFilePanel() {
		JPanel panel = new JPanel();
		panel.setSize(width, 22);
		panel.setLayout(null);
		panel.setBounds(0, 200, width, 53);
		initSelectedFileField();
		JLabel label = new JLabel("Select file to translate:");
		label.setBounds(10, 0, width, 22);
		panel.add(label);
		panel.add(selectedFileField);
		panel.add(getOpenButton());
		return panel;
	}

	private JButton getOpenButton() {
		openButton = new JButton("Browse...");
		openButton.addActionListener(openPropertyFileLister);
		openButton.setBounds(270, 25, 120, 22);
		return openButton;
	}

	private void initSelectedFileField() {
		selectedFileField.setEditable(false);
		selectedFileField.setBackground(Color.WHITE);
		selectedFileField.setBounds(10, 25, 250, 22);
	}

	private void initSourceLanguageList() {
		sourceLanguageList = new JList(languages);
		sourceLanguageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		sourceLangSrollPane = new JScrollPane(sourceLanguageList);
		sourceLanguageList.setSelectedValue(Language.GERMAN.name(), true);
		sourceLangSrollPane.setBounds(10, 50, 170, 150);
		sourceLangSrollPane.setSize(170, 100);
		sourceLangSrollPane.setBorder(null);
		sourceLanguageList.addMouseListener(clickListener);
	}

	private void initTargetLanguageList() {
		targetLanguageList = new JList(languages);
		targetLanguageList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		targetLangSrollPane = new JScrollPane(targetLanguageList);
		targetLanguageList.setSelectedValue(Language.ENGLISH.name(), true);
		targetLangSrollPane.setBounds(210, 50, 170, 150);
		targetLangSrollPane.setSize(170, 100);
		targetLangSrollPane.setBorder(null);
		targetLanguageList.addMouseListener(clickListener);
	}

	private JMenuBar getMenu() {
		JMenuBar bar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		JMenuItem menuItem = new JMenuItem("Open property file");
		menuItem.addActionListener(openPropertyFileLister);
		fileMenu.add(menuItem);
		fileMenu.addSeparator();
		menuItem = new JMenuItem("Exit");
		menuItem.addActionListener(new ExitApplicationListener());
		fileMenu.add(menuItem);
		bar.add(fileMenu);
		return bar;
	}

	public File getSourceFile() {
		return openPropertyFileLister.getSourceFile();
	}

	public Language getSourceLanguage() {
		String selectedLanguage = (String) sourceLanguageList.getSelectedValue();
		return LanguageManager.getLanguage(selectedLanguage);
	}

	public Language[] getTargetLanguages() {
		Object[] test = targetLanguageList.getSelectedValues();
		String[] selectedLanguages = new String[test.length];
		int i = 0;
		for (Object o : test) {
			String selectedLanguage = (String) o;
			selectedLanguages[i++] = selectedLanguage;
		}
		return LanguageManager.getLanguages(selectedLanguages);
	}

	public void enableTranslateButton(boolean enable) {
		translateButton.setEnabled(enable);
	}

	public void enableOpenButton(boolean enable) {
		openButton.setEnabled(enable);
	}

	public void setPbarValue(int value) {
		pbar.setValue(value);
	}

	public void setPbarString(String text) {
		pbar.setString(text);
	}

	public void increasePbarValue() {
		int value = pbar.getValue() + 1;
		pbar.setValue(value);
	}

	public void setPbarMaximum(int value) {
		pbar.setMaximum(value);
	}

	public void resetPbar() {
		pbar.setValue(0);
	}

	public void setStatus(String status) {
		statusLabel.setText(status);
	}

	private boolean isSourceLangSelected() {
		Object test = sourceLanguageList.getSelectedValue();
		if (test == null) {
			return false;
		}
		return true;
	}

	private boolean isTargetLangSelected() {
		Object[] test = targetLanguageList.getSelectedValues();
		if (test == null || test.length == 0) {
			return false;
		}
		return true;
	}

	private boolean isFileSelected() {
		if (getSourceFile() == null) {
			return false;
		}
		return true;
	}

	public void validateGUI() {
		if (!isSourceLangSelected() || !isTargetLangSelected() || !isFileSelected()) {
			enableTranslateButton(false);
		} else {
			enableTranslateButton(true);
		}
	}

	public void resetGUI() {
		selectedFileField.setText("");
		openButton.setEnabled(true);
		translateButton.setEnabled(false);
		openPropertyFileLister.reset();
	}
}
